package problem1658;


//1658.将x减到0的最小操作数
//https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/description/

class Solution {
    public int minOperations(int[] nums, int x) {
        int n = nums.length;
        System.out.println(n);
        int[] arr = new int[n*2];
        for(int i = 0; i<n; i++) arr[i] = nums[i];
        for(int i = n; i<2*n; i++) arr[i] = nums[i-n];
        int ret = Integer.MAX_VALUE;
        for(int i = 0, j = 0; j<2*n; j++) {
            x -= arr[j];
            while(x < 0) {
                x += arr[i];
                i++;
            }
            if((x == 0) && (j-i+1 <= n) && (i == 0 || j >= n-1) && i < n) {
                ret = Math.min(ret, j-i+1);
            }
        }
        return ret==Integer.MAX_VALUE ? -1 : ret;
    }
}

/**

3,2,20,1,1,3, 3,2,20,1,1,3,

1,1,4,2,3 ,1,1,4,2,3
 */